package ve.usb.coord.colision;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.coord.colision.hibernate.*;


/**
 * Manejo de elementos de la entidad Departamento.<br>
 */
public class AccionesDepartamento extends CohesionAction {
    
/**
     * Acción llamada por Struts para ejecutar la acción DepartamentoBuscarI.
     * Prepara la vista de selección de criterios de búsqueda para una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward DepartamentoBuscarI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "departamentoBuscar";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            HttpSession sess = request.getSession();
            {FDepartamentoBuscar fFDepartamentoBuscar = (FDepartamentoBuscar)sess.getAttribute("FDepartamentoBuscar");
             if (fFDepartamentoBuscar==null) {
                 fFDepartamentoBuscar = new FDepartamentoBuscar();
                 sess.setAttribute("FDepartamentoBuscar", fFDepartamentoBuscar);
             } else {
                 fFDepartamentoBuscar.reset(mapping, request);
             }
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción DepartamentoCrearI.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward DepartamentoCrearI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "departamentoCrear";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            HttpSession sess = request.getSession();
            {FDepartamento fFDepartamento = (FDepartamento)sess.getAttribute("FDepartamento");
             if (fFDepartamento==null) {
                 fFDepartamento = new FDepartamento();
                 sess.setAttribute("FDepartamento", fFDepartamento);
             } else {
                 fFDepartamento.reset(mapping, request);
             }

            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción DepartamentoBuscar.
     * Captura los criterios de búsqueda y los fija en la sesión.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward DepartamentoBuscar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "DepartamentoListarI";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FDepartamentoBuscar fFDepartamentoBuscar = (FDepartamentoBuscar)form;

            request.getSession().setAttribute("DepartamentoBuscar", " where nombre like '%" + fFDepartamentoBuscar.getNombre() + "%'");

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción DepartamentoListarI.
     * Selecciona las entidades a ser listadas de acuerdo a los parámetros recibidos.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward DepartamentoListarI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "departamentoListar";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String crit = (String)request.getSession().getAttribute("DepartamentoBuscar");
            if (crit==null) crit = "";
            else request.getSession().removeAttribute("DepartamentoBuscar");
            java.util.List<Departamento> list = (java.util.List<Departamento>)s.createQuery("from Departamento"+crit).list();
            request.setAttribute("L_Departamento", list);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción DepartamentoI.
     * Prepara la vista de actualización de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward DepartamentoI(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "departamento";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idDepartamento");
            Long idDepartamento = (id==null) ? 
                (Long)request.getSession().getAttribute("idDepartamento") :
                new Long(id);
            
            HttpSession sess = request.getSession();
            FDepartamento fFDepartamento = (FDepartamento)sess.getAttribute("FDepartamento");
            if (fFDepartamento==null) {
                fFDepartamento = new FDepartamento();
                sess.setAttribute("FDepartamento", fFDepartamento);
            } else {
                fFDepartamento.reset(mapping, request);
            }
            Departamento departamento = (Departamento)s.load(Departamento.class, idDepartamento);
            fFDepartamento.setIdDepartamento(departamento.getIdDepartamento());
            fFDepartamento.setNombre(departamento.getNombre());
            fFDepartamento.setDireccion(departamento.getDireccion());
            fFDepartamento.setTelefono(departamento.getTelefono());

            Hibernate.initialize(departamento.getAsignaturas());
            request.setAttribute("L_Asignatura", departamento.getAsignaturas());

            request.getSession().setAttribute("idDepartamento", idDepartamento);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción DepartamentoAct.
     * Registro de actualizaciones de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward DepartamentoAct(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "departamento";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FDepartamento fFDepartamento = (FDepartamento)form;

            Departamento departamento = (Departamento)s.load(Departamento.class, new Long(fFDepartamento.getIdDepartamento()));
            departamento.setNombre(fFDepartamento.getNombre());
            departamento.setDireccion(fFDepartamento.getDireccion());
            departamento.setTelefono(fFDepartamento.getTelefono());


            Hibernate.initialize(departamento.getAsignaturas());
            request.setAttribute("L_Asignatura", departamento.getAsignaturas());

            request.getSession().setAttribute("idDepartamento", departamento.getIdDepartamento());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción DepartamentoElim.
     * Elimina la entidad seleccionada.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward DepartamentoElim(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_0 = "DepartamentoListarI";
        final String SALIDA_1 = "departamento";

        String salida = SALIDA_0;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {

            String id = request.getParameter("idDepartamento");
            Long idDepartamento = (id==null) ? 
                (Long)request.getSession().getAttribute("idDepartamento") :
                new Long(id);
            Departamento departamento = (Departamento)s.load(Departamento.class, idDepartamento);
            s.delete(departamento);
            try {
                s.flush();
            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
                request.setAttribute("msg", "No se pudo eliminar entidad.");
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }

/**
     * Acción llamada por Struts para ejecutar la acción DepartamentoCrear.
     * Registro de la creación de una departamento.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward DepartamentoCrear(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        //Salidas
        final String SALIDA_1 = "departamento";

        String salida = SALIDA_1;        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FDepartamento fFDepartamento = (FDepartamento)form;

            Departamento departamento = new Departamento();
            departamento.setNombre(fFDepartamento.getNombre());
            departamento.setDireccion(fFDepartamento.getDireccion());
            departamento.setTelefono(fFDepartamento.getTelefono());

            s.save(departamento);
            s.flush();
            fFDepartamento.setIdDepartamento(departamento.getIdDepartamento());

            request.getSession().setAttribute("idDepartamento", departamento.getIdDepartamento());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(salida);
    }



}